package com.librarysys.controller;

import com.librarysys.common.Const;
import com.librarysys.common.Result;
import com.librarysys.dao.BorrowMapper;
import com.librarysys.dao.PurviewMapper;
import com.librarysys.entity.Manager;
import com.librarysys.entity.Purview;
import com.librarysys.service.IBorrowService;
import com.librarysys.vo.BorrowVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by yuhui on 2017/7/13.
 */
@Controller
@RequestMapping("/borrow")
public class BorrowController {
    @Autowired
    private PurviewMapper purviewMapper;

    @Autowired
    private IBorrowService iBorrowService;

    @RequestMapping("/reader_info")
    public String readerInfo(String barcode, HttpServletRequest request){
        if(comfirm(request.getSession())){
            Result readerInfo = iBorrowService.getReaderInfo(barcode);
            request.setAttribute("msg",readerInfo.getMsg());
            if(readerInfo.isSuccess()){
                request.setAttribute("reader",readerInfo.getData());
            }
            return "bookBorrow";
        }else{
            return "no_access";
        }
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<BorrowVO> borrowList(String type,String value){
            return (List<BorrowVO>) iBorrowService.borrowList(type,value).getData();
    }
    private Boolean comfirm(HttpSession session){
        Manager manager = (Manager) session.getAttribute(Const.CURRENT_USER);
        Purview purview = purviewMapper.selectByPrimaryKey(manager.getId());
        if(purview==null){
            return false;
        }
        if(purview.getBorrowback()){
            return true;
        }
        return false;
    }

    @RequestMapping("/finish")
    public String borrowFinish(Integer readerId,String bookIds,HttpServletRequest request){
            if(!comfirm(request.getSession())){
                return "no_access";
            }

            request.setAttribute("suc","借书成功");
            return "bookBorrow";
    }
}
